package top.continew.admin.generator.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.continew.admin.config.CloudSmsConfig;
import top.continew.admin.generator.service.SmsService;

/**
 * @description:
 * @author: jiangjun
 * @date: 2025/6/20 14:14
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SmsServiceImpl implements SmsService {

    private static final String SMS_NOTICE_TEMPLATE_ID = "2515160";

    private final CloudSmsConfig cloudSmsConfig;

    private SmsClient client;

    @PostConstruct
    public void init() {
        Credential cred = new Credential(cloudSmsConfig.getTencentConfig().getSecretId(), cloudSmsConfig.getTencentConfig().getSecretKey());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setEndpoint(cloudSmsConfig.getTencentConfig().getEndpoint());
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        this.client = new SmsClient(cred, cloudSmsConfig.getTencentConfig().getRegion(), clientProfile);
    }


    @Override
    public void sendNotice(String[] phoneNumbers, Integer taskNum) throws Exception {
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(cloudSmsConfig.getTencentConfig().getAppId());
        req.setSignName(cloudSmsConfig.getTencentConfig().getSignName());
        req.setTemplateId(SMS_NOTICE_TEMPLATE_ID);
        req.setPhoneNumberSet(phoneNumbers);
        SendSmsResponse sendSmsResponse = client.SendSms(req);
        log.info("SendSmsResponse: {}", SendSmsResponse.toJsonString(sendSmsResponse));
    }
}
